<template>
  <div class="form-layout">
    <!--左侧导航-->
    <div class="nav-left">
      <customAnchor
        :itemList="[
          { name: 'box-base', title: '设备信息' },
          { name: 'box-work', title: '工作情况' },
          { name: 'box-in', title: '收入类' },
          { name: 'box-out', title: '支出类' },
        ]"
      />
    </div>
    <!--表单区域-->
    <div class="form-right" ref="formRight">
      <customForm :data="form" :rules="propRules" ref="editForm" :disabled="propReadonly">
        <template #content>
          <div id="box-base" class="box-title">基础信息</div>
          <customFormItem label="设备名称" name="deviceName" :parent="_self" :form="form"
              :readonly="true"
              :showSelectIcon="true"
              @focus="utilSelect.deviceOne(deviceSelect, '', '', '', 'benefit_settings', '未添加到设备配置', '', {})"
              :disabled="!propParent.cpFormNew"
          />
          <customFormItem label="设备编码" name="deviceCode" :parent="_self" :form="form" disabled/>
          <customFormItem label="标签码" name="deviceSn" :parent="_self" :form="form" disabled/>
          <customFormItem label="所属科室" name="deptNameOwner" :parent="_self" :form="form" disabled/>

          <div id="box-work" class="box-title">工作情况</div>
          <customFormItem label="单机消费" name="flagAlone" :parent="_self"  :form="form" :type="constCommon.formItemType.switch.name"/>
          <customFormItem label="满负荷工作量" name="workloadFull" :parent="_self" :form="form"  :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="预计工作量" name="workloadReality" :parent="_self" :form="form"  :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="备注" name="remark" :parent="_self" :form="form" />

          <div id="box-in" class="box-title">收入类</div>
          <customFormItem label="总收入" name="incomeTotal" :parent="_self" :form="form"  :type="constCommon.formItemType.slot.name">
            <template #content>
              <strong style="color: #009966;font-size: 22px;">{{form.incomeTotal}}</strong>
            </template>
          </customFormItem>
          <customFormItem label="住院收入" name="incomeInpatient" :parent="_self" :form="form"  :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="门诊收入" name="incomeOutpatient" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="药费收入" name="profitsDrug" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="检查收入" name="profitsInspect" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="材料费收入" name="profitsMaterial" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="治疗收入" name="profitsTreat" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="其他收入" name="incomeOther" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>

          <div id="box-out" class="box-title">支出类</div>
          <customFormItem label="总支出" name="expendTotal" :parent="_self" :form="form"    :type="constCommon.formItemType.slot.name">
            <template #content>
              <strong style="color: #FF6666;font-size: 22px;">{{form.expendTotal}}</strong>
            </template>
          </customFormItem>
          <customFormItem label="检测支出" name="expendCheck" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="折旧费支出" name="expendDepr" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="药费费支出" name="expendDrug" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="利息支出" name="expendInterest" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="人工成本支出" name="expendLabor" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="维保费支出" name="expendMaint" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="管理费支出" name="expendManage" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="材料费支出" name="expendMaterial" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="计量支出" name="expendMmetering" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="维修费支出" name="expendRepair" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="场地费支出" name="expendSite" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
          <customFormItem label="水电费支出" name="expendUtilities" :parent="_self" :form="form" :type="constCommon.formItemType.inputNumber.name"/>
        </template>
      </customForm>
    </div>
  </div>
</template>

<script>
import customAnchor from '@/components/custom/form/customAnchor.vue'
export default {
  desc: '效益分析设备基础配置-详情',
  data() {
    return {
      form: {},

      // 收入字段
      nameInList: ['incomeInpatient', 'incomeOutpatient', 'profitsDrug', 'profitsInspect', 'profitsMaterial', 'profitsTreat', 'incomeOther'],

      // 支出字段
      nameOutList: ['expendCheck','expendDepr','expendDrug','expendInterest','expendLabor','expendMaint','expendManage','expendMaterial','expendMmetering','expendRepair','expendSite','expendUtilities'],
    }
  },
  computed: {
    //总收入
    cpSumIn() {
      let sum = 0
      let sumOne = 0
      Object.keys(this.nameInList).forEach(index => {
        sumOne = this.form[this.nameInList[index]]
        if (this.utilNum.isNum(sumOne)) {
          sum += Number(sumOne)
        }
      })
      return this.utilNum.toFixed(sum, 4)
    },
    //总支出
    cpSumOut() {
      let sum = 0
      let sumOne = 0
      Object.keys(this.nameOutList).forEach(index => {
        sumOne = this.form[this.nameOutList[index]]
        if (this.utilNum.isNum(sumOne)) {
          sum += Number(sumOne)
        }
      })
      return this.utilNum.toFixed(sum, 4)
    }
  },
  created() {
    this.$emit('event-register', {
      //表单校验
      validate: (callback) => {
        return this.$refs.editForm.validate(callback)
      },

      //保存前操作
      beforeSave: () => {
        this.$emit('data-callback', this.form)
        return true
      },

      //数据加载后
      afterMove: (formData, initData) => {
        this.form = global.utils.jsonClone(formData)
      },

      //数据保存后
      afterSave: () => {
      },
      beforeDelete: () => {
        return true
      },

      //数据删除后
      afterDelete: () => {
      },

      //流程提交前
      beforeFlow: (flowButtonKey) => {
        return true
      },

      //流程提交后
      afterFlow: (flowButtonKey) => {
      },

      //表单关闭前
      beforeClose: () => {
        return true
      }
    })
  },
  props: {
    //上级对象
    propParent: Object,
    //上级初始化数据
    propInitData: Object,
    //主键字段
    propPrimaryKey: String,
    //数据字典
    propDictionaries: Object,
    //当前操作行
    propCurrentRow: Object,
    //表单数据
    propViewFormData: Object,
    //是否只读
    propReadonly: Boolean,
    //操作状态
    propStatus: String,
    //表单规则
    propRules: Object
  },
  components: {
    customAnchor
  },
  watch: {
    // 监听
    cpSumIn: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.$set(this.form, 'incomeTotal', this.cpSumIn)
      },
    },
    // 监听
    cpSumOut: {
      immediate: true, // 初始创建 watch 时立即执行回调函数
      deep: true, // 监听到对象里面的值的变化
      handler() {
        this.$set(this.form, 'expendTotal', this.cpSumOut)
      },
    }
  },
  methods: {
    // 设备选择
    deviceSelect(row) {
      this.$set(this.form, 'ledgerId', row.id)
      let setArr = ['deviceName','deviceCode','deviceSn','deptNameOwner','deptIdOwner']
      Object.keys(setArr).forEach(index => {
        this.$set(this.form, setArr[index], row[setArr[index]])
      })
    }
  }
}
</script>
